package com.soft.collateral.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soft.collateral.constant.MessageConstant;
import com.soft.collateral.entity.Agencies;
import com.soft.collateral.entity.User;
import com.soft.collateral.result.Result;
import com.soft.collateral.service.AgenciesService;
import com.soft.collateral.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 外估机构表 前端控制器
 * </p>
 *
 * @author liubin
 * @since 2025-07-25
 */
@RestController
@RequestMapping("/admin/agencies")
@Slf4j
@Api(tags = "后台系统-外估机构接口")
@RequiredArgsConstructor
public class AgenciesController {

    private final AgenciesService agenciesService;

    @ApiOperation("分页查询外估机构")
    @PostMapping("page")
    public Result page(@RequestBody PageQueryVO vo) {

        Page<Agencies> agenciesPage = agenciesService.page(vo);
        if(agenciesPage!=null && agenciesPage.getTotal()>0){
            return new Result(true , MessageConstant.GET_AGENCIES_SUCCESS , agenciesPage);
        }
        return new Result(false , MessageConstant.GET_AGENCIES_FAIL);
    }

    @ApiOperation("新增外估机构")
    @PostMapping("create")
    public Result create(@RequestBody Agencies agencies) {

        boolean saveAgencies = agenciesService.saveAgencies(agencies);
        if(saveAgencies){
            return new Result(true , MessageConstant.SAVE_AGENCIES_SUCCESS);
        }
        return new Result(false , MessageConstant.SAVE_AGENCIES_FAIL);
    }

    @ApiOperation("更新外估机构信息")
    @PutMapping("update")
    public Result update(@RequestBody Agencies agencies){

        boolean flag = agenciesService.updateById(agencies);
        if(flag){
            return new Result(true ,MessageConstant.UPDATE_AGENCIES_SUCCESS);
        }
        return new Result(false ,MessageConstant.UPDATE_AGENCIES_FAIL);
    }

    @ApiOperation("根据id删除外估机构")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable Integer id) {

        boolean flag = agenciesService.removeById(id);
        if(flag){
            return new Result(true ,MessageConstant.DELETE_AGENCIES_SUCCESS);
        }
        return new Result(false ,MessageConstant.DELETE_AGENCIES_FAIL);
    }

}
